package com.mango.idea;

import com.intellij.ide.ApplicationInitializedListener;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.ui.Messages;
import org.apache.commons.lang3.StringUtils;

import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.lang.management.RuntimeMXBean;

/**
 * 启动信息
 * @Author: mango
 * @Date: 2022/8/14 11:41 上午
 */
public class MyApplicationInitializedListener implements ApplicationInitializedListener {
    @Override
    public void componentsInitialized() {
        RuntimeMXBean bean = ManagementFactory.getRuntimeMXBean();
        long startTime = bean.getStartTime();
        long costTime = System.currentTimeMillis() - startTime;
        System.out.println("耗时"+costTime+"ms");
        ApplicationManager.getApplication().invokeLater(()->{
            Messages.showMessageDialog(StartBean.showInfo(costTime), "启动信息", Messages.getInformationIcon());
        });
    }

    static class StartBean {
        // 启动耗时
        private long startCostMs;
        // 虚拟机名称
        private String vmName;
        // 虚拟机供应商
        private String vmVendor;
        // 虚拟机版本
        private String vmVersionStr;
        // 堆初始化大小，单位兆
        private long heapInitSize;
        // 堆使用情况
        private long heapUsedSize;
        // 堆最大情况
        private long heapMaxSize;
        // 使用的GC
        private String useGCName;

        public static String showInfo(long startCostMs){
            StartBean startBean = new StartBean();
            startBean.startCostMs = startCostMs;
            RuntimeMXBean bean = ManagementFactory.getRuntimeMXBean();
            startBean.vmName = bean.getVmName();
            startBean.vmVendor = bean.getVmVendor();
            startBean.vmVersionStr = bean.getSpecVersion() + " " + bean.getVmVersion();
            MemoryMXBean mxb = ManagementFactory.getMemoryMXBean();
            System.out.println(mxb.getHeapMemoryUsage().toString());
            startBean.heapInitSize = mxb.getHeapMemoryUsage().getInit() / 1024 / 1024;
            startBean.heapUsedSize = mxb.getHeapMemoryUsage().getUsed() / 1024 / 1024;
            startBean.heapMaxSize = mxb.getHeapMemoryUsage().getMax() / 1024 / 1024;
            // 通过VM参数截取GC配置
            bean.getInputArguments().forEach(e->{
                if(e.startsWith("-XX:+Use") && e.endsWith("GC")){
                    startBean.useGCName = e.substring(8);
                }
            });
            return startBean.format();
        }

        private String format() {
            StringBuilder sb = new StringBuilder();
            sb.append("启动耗时：" + this.startCostMs + "ms");
            sb.append("\r\n");
            sb.append("虚拟机名称：" + this.vmName);
            sb.append("\r\n");
            sb.append("虚拟机供应商：" + this.vmVendor);
            sb.append("\r\n");
            sb.append("虚拟机版本：" + this.vmVersionStr);
            sb.append("\r\n");
            sb.append("堆最大内存：" + this.heapMaxSize + "M");
            sb.append("\r\n");
            sb.append("堆初始化内存：" + this.heapInitSize + "M");
            sb.append("\r\n");
            sb.append("堆已使用内存：" + this.heapUsedSize + "M");
            if(StringUtils.isNotBlank(this.useGCName)){
                sb.append("\r\n");
                sb.append("使用的GC：" + this.useGCName);
            }
            return sb.toString();
        }
    }
}